<template>
  <div class="nav-bar-container">
    <div
      class="nav-bar-item-active"
      :style="{ top: `${selectId * height}px` }"
    />
    <div class="nav-bar-content">
      <div
        :class="[
          'nav-bar-item',
          selectId == index ? 'active' : null,
        ]"
        :style="{
          height: `${height}px`,
        }"
        v-for="(item, index) in list"
        :key="index"
        @click="handleItem(item, index)"
      >
        <slot :data="item"></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    height: {
      type: Number,
      default: 36,
    },
    list: {
      type: Array,
    },
    selectIndex: {
      type: Number,
      default: 0
    },
  },
  data() {
    return {
      selectId: this.selectIndex,
    };
  },
  methods: {
    handleItem(item, index) {
      if (this.selectId != index) {
        this.selectId = index;
        this.$emit("change", item);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.nav-bar-container {
  position: relative;
  width: 100%;
  margin-top: 10px;

  .nav-bar-item-active {
    position: absolute;
    top: 0;
    z-index: 1;
    width: 100%;
    height: 36px;
    border-radius: 4px;
    background-color: var(--background-hover);
    transition: all 0.28s;
  }
}

.nav-bar-content {
  position: absolute;
  width: 100%;
  top: 0;
  color: var(--font-color);
  z-index: 2;

  .nav-bar-item {
    width: 100%;
    height: 36px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;

    .nav-icon {
      width: 24px;
      height: 24px;
      color: #eee;
    }

    .nav-title {
      margin: 0 8px;
    }
  }

  .active {
    color: var(--font-hover);

    .nav-icon {
      width: 24px;
      height: 24px;
    }
  }
}
</style>